package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.MachineAbnormalDao;
import com.tbit.main.pojo.MachineAbnormal;
import com.tbit.main.pojo.OrderJob;
import com.tbit.main.service.MachineAbnormalService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 车辆故障
 *
 * @author Leon
 * 2019年11月16日 下午2:56:53
 */
@Service("machineAbnormalService")
public class MachineAbnormalServiceImpl implements MachineAbnormalService {
    @Autowired
    private MachineAbnormalDao machineAbnormalDao;

    @Override
    public List<MachineAbnormal> getByMachineId(Integer machineId) {
        return machineAbnormalDao.getByMachineId(machineId);
    }

    @Override
    public List<MachineAbnormal> getAll() {
        return machineAbnormalDao.getAll();
    }

    @Override
    public MachineAbnormal getByMachineIdAndType(Integer machineId, Integer abnormalType) {
        Map<String, Object> params = new HashMap<>();
        params.put("machineId", machineId);
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.getByMachineIdAndType(params);
    }

    @Override
    public List<Integer> getByMachineIdsIdAndType(List<Integer> machineIds, List<Integer> abnormalTypes) {

        List<Integer> machineIdList = new LinkedList<>();

        List<List<Integer>> machineId = ListUtils.partition(machineIds, 900);
        for (List<Integer> id : machineId) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("machineIds", StringUtil.getListString(id));
            params.put("abnormalType", StringUtil.getListString(abnormalTypes));
            machineIdList.addAll(machineAbnormalDao.getByMachineIdsIdAndType(params));
        }

        return machineIdList;
    }

    @Override
    public void insert(MachineAbnormal machineAbnormal) {
        machineAbnormal.setAbnormalTime(DateTimeUtil.getNowTime());
        machineAbnormalDao.insert(machineAbnormal);
    }

    @Override
    public void del(Integer machineId, Integer abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("machineId", machineId);
        params.put("abnormalType", abnormalType);

        machineAbnormalDao.del(params);
    }

    @Override
    public void deleteBatch(List<Integer> machineIds, Integer abnormalType) {
        List<List<Integer>> userCodes = ListUtils.partition(machineIds, 900);
        for (List<Integer> code : userCodes) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("machineIds", StringUtil.getListString(code));
            params.put("abnormalType", abnormalType);
            machineAbnormalDao.deleteBatch(params);
        }
    }

    @Override
    public void delAccountId(Integer accountId, Integer abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        machineAbnormalDao.delAccountId(params);
    }

    @Override
    public List<MachineAbnormal> groupByMachineIdAndType(Integer accountId, Integer abnormalType, String addTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        params.put("addTime", addTime);
        return machineAbnormalDao.groupByMachineIdAndType(params);
    }

    @Override
    public List<MachineAbnormal> groupMachineIdAndType(Integer accountId, Integer abnormalType, String addTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        params.put("addTime", addTime);
        return machineAbnormalDao.groupMachineIdAndType(params);
    }

    @Override
    public List<MachineAbnormal> groupByMachineIdAndRegionId(List<Integer> regionId, Integer abnormalType, String addTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("abnormalType", abnormalType);
        params.put("addTime", addTime);
        return machineAbnormalDao.groupByMachineIdAndRegionId(params);
    }

    @Override
    public List<MachineAbnormal> groupMachineIdAndRegionId(List<Integer> regionId, Integer abnormalType, String addTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("abnormalType", abnormalType);
        params.put("addTime", addTime);
        return machineAbnormalDao.groupMachineIdAndRegionId(params);
    }

    @Override
    public List<MachineAbnormal> groupByMachineIdAndListType(Integer accountId, List<Integer> abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.groupByMachineIdAndListType(params);
    }

    @Override
    public List<MachineAbnormal> groupMachineIdAndListType(Integer accountId, List<Integer> abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.groupMachineIdAndListType(params);
    }

    @Override
    public List<MachineAbnormal> groupByMachineIdRegionIdAndType2(List<Integer> regionId, List<Integer> abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("regionId", StringUtil.getListString(regionId));
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.groupByMachineIdRegionIdAndType2(params);
    }

    @Override
    public List<MachineAbnormal> getAccountIdType(Integer abnormalType, Integer accountId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.getAccountIdType(params);
    }

    @Override
    public List<MachineAbnormal> getByType(List<Integer> abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("abnormalType", StringUtil.getListString(abnormalType));
        return machineAbnormalDao.getByType(params);
    }

    @Override
    public List<MachineAbnormal> getOrderJob(List<OrderJob> orderJobList) {
        return machineAbnormalDao.getOrderJob(orderJobList);
    }

    @Override
    public List<MachineAbnormal> getByAccountId(Integer accountId) {
        return machineAbnormalDao.getByAccountId(accountId);
    }

    @Override
    public List<MachineAbnormal> getByAccountIds(List<Integer> accountIds) {
        return machineAbnormalDao.getByAccountIds(StringUtil.getListString(accountIds));
    }

    @Override
    public List<Map<String, Object>> getGroupByAccountId() {
        return machineAbnormalDao.getGroupByAccountId();
    }

    @Override
    public Integer getByAccountIdCount(Integer accountId) {
        return machineAbnormalDao.getByAccountIdCount(accountId);
    }

    @Override
    public void insertBatch(List<Integer> machineId, Integer abnormalType, String dateTime) {
        List<List<Integer>> userCodes = ListUtils.partition(machineId, 900);
        for (List<Integer> code : userCodes) {
            machineAbnormalDao.insertBatch(code, abnormalType, dateTime);
        }
    }

    @Override
    public List<Integer> getByAbnormalType(Integer abnormalType) {
        return machineAbnormalDao.getByAbnormalType(abnormalType);
    }

    @Override
    public List<Map<String, Object>> getAccountIdAndType() {
        return machineAbnormalDao.getAccountIdAndType();
    }

    @Override
    public Integer getAccountIdTypeCount(Integer accountId, Integer abnormalType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("abnormalType", abnormalType);
        return machineAbnormalDao.getAccountIdTypeCount(params);
    }

    @Override
    public List<MachineAbnormal> getByMachineIds(List<Integer> machineIds) {
        List<MachineAbnormal> machineAbnormals = new LinkedList<>();

        List<List<Integer>> machineId = ListUtils.partition(machineIds, 900);
        for (List<Integer> id : machineId) {
            machineAbnormals.addAll(machineAbnormalDao.getByMachineIds(StringUtil.getListString(id)));
        }
        return machineAbnormals;
    }
}
